package com.avic.rong;


import android.app.Activity;

import android.os.Bundle;
import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.utils.SharedPreferencesUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class  BaseFragment extends Fragment {


    protected View fragmentView;
    protected Activity context;
    protected ViewGroup containerView;
    protected LayoutInflater mInflater;

    /**
     * 绑定,解绑butterknife
     */
    private Unbinder unbinder;
    public boolean isRefresh = false;
    public Integer currentPageNumber = 1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (fragmentView == null) {
            fragmentView = inflater.inflate(getContentViewId(), container, false);
            unbinder = ButterKnife.bind(this, fragmentView);

            context = getActivity();
            mInflater = LayoutInflater.from(context);

            initView();
            addListener();
        }
        unbinder = ButterKnife.bind(this, fragmentView);


        return fragmentView;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        if (mInflater == null) {
            mInflater = LayoutInflater.from(context);
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /***
     * 初始化view
     */

    protected abstract void initView();

    /***
     * 注册监听器
     */
    protected abstract void addListener();

    private SharedPreferencesUtil sharedPreferencesUtil;

    protected SharedPreferencesUtil getSharedPreferencesUtil() {
        if (sharedPreferencesUtil == null) {
            sharedPreferencesUtil = SharedPreferencesUtil.getInstance(context);
        }
        return sharedPreferencesUtil;
    }


    /***
     * 获取布局
     *
     * @return
     */
    protected abstract int getContentViewId();




    protected <T extends View> T $(int id) {
        return (T) fragmentView.findViewById(id);
    }


}
